package editmailbox;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use base 'func';
use Data::Validate::Domain qw(is_domain);
use Data::Validate::Email qw(is_email);
use strict;
use CGI::Carp qw(fatalsToBrowser);

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub return_edit_mailbox {

    my $self = shift;

    my $dd;

    if (   ( !length( $self->{p}->{domain} ) )
        || ( 4 > length( $self->{p}->{domain} ) )
        || ( 250 < length( $self->{p}->{domain} ) )
        || !is_domain( $self->{p}->{domain} ) )
    {
        $self->{he}->{domain_correct} = 1;
    }

    if (   ( !length( $self->{p}->{mailbox} ) )
        || ( 100 < length( $self->{p}->{mailbox} ) )
        || ( !is_email( $self->{p}->{mailbox} ) ) )
    {
        $self->{he}->{username_correct} = 1;
    }

    if (   $self->{he}->{username_correct} != 1
        && $self->{he}->{domain_correct} != 1 )
    {

        my $cl;
        my @sql = ();
        if ( $self->{session}->{name} eq 'Admin' ) {
            push @sql, $self->{p}->{domain}, $self->{p}->{mailbox};
        }
        else {
            $cl = 'AND t2.idnane = ?';
            push @sql, $self->{p}->{domain}, $self->{p}->{mailbox},
              $self->{session}->{user};
        }

        my $sth = $self->{dbh}->prepare(
            "SELECT t1.username,
                                t1.name,
                                t1.quota,
                                t1.domain

                         FROM mailbox AS t1,
                              domain AS t2                    
                         WHERE t1.domain = ?
                         AND t1.username = ?
                         $cl
                         AND t1.domain = t2.domain 
                         LIMIT 1
                          "
        );
        $sth->execute(@sql);

        $dd = $sth->fetchrow_hashref();
        $sth->finish();
    }

    if ( $dd < 1 ) {
        $self->{he}->{NOTS} = 1;
    }
    else {

        ( $dd->{only_username} ) = split( /@/, $dd->{username} );

        while ( my ( $key, $value ) = each( %{$dd} ) ) {

            if ( $key eq 'quota' && $value > 0 ) {
                $value = $value / 1048576;
                $value = int($value);
            }

            $self->{he}->{$key} = $value;

        }
    }

    return $self;
}

sub templ_hash_mailbox {
    my $self = shift;
    $self->{session}->{filet} = 'edit-mailbox';
    $self->SUPER::templ_hash(@_);
    return $self;
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
